<?php

namespace app\index\controller;

use app\common\controller\Frontend;

/**
 * 首页控制器
 * Class Index
 * @package app\index\controller
 */
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function index()
    {
        //根据当前控制器标识去拿keywords 和description
        $pageInfo=db('navigation')->where('controllername="'.$this->identify.'"')->field('title,keywords,description')->find();

        //banner
        $bannerList=db('banner')->order('sort desc,id asc')->field('title,thumb,link')->select();

        //商务合作
        $businessCooperationList=db('business_cooperation')->order('sort desc,id asc')->field('title,thumb,link')->select();

        //广告合作
        $advertisingCooperationList=db('advertising_cooperation')->order('sort desc,id asc')->field('title,link')->select();

        //分类文章展示1
        //分类列表
        $categoryList1=db('article_category')->where('isindex=1')->order('sort desc,id asc')->field('id,name')->limit(3)->select();
        //取第一个分类文章数据作为默认展示
        if(!empty($categoryList1)){
            $articleList1=db('article')->where('isindex=1 and category_id='.$categoryList1[0]['id'])->order('sort desc,id desc')->field('id,title')->limit(15)->select();
            $this->assign('articleList1',$articleList1);
        }

        //分类文章展示2
        $categoryList2=db('article_category')->where('isindex=2')->order('sort desc,id asc')->field('id,name')->limit(3)->select();
        if(!empty($categoryList2)){
            $articleList2=db('article')->where('isindex=1 and category_id='.$categoryList2[0]['id'])->order('sort desc,id desc')->field('id,title')->limit(15)->select();
            $this->assign('articleList2',$articleList2);
        }

        //分类文章展示3
        $categoryList3=db('article_category')->where('isindex=3')->order('sort desc,id asc')->field('id,name')->limit(3)->select();
        if(!empty($categoryList3)){
            $articleList3=db('article')->where('isindex=1 and category_id='.$categoryList3[0]['id'])->order('sort desc,id desc')->field('id,title,isding,thumb,info,createtime,author,avatar')->limit(4)->select();
            $this->assign('articleList3',$articleList3);
        }

        //分类文章展示4
        $categoryList4=db('article_category')->where('isindex=4')->order('sort desc,id asc')->field('id,name')->find();
        if(!empty($categoryList4)){
            $articleList4=db('article')->where('isindex=1 and category_id='.$categoryList4['id'])->order('sort desc,id desc')->field('id,title,thumb,from_unixtime(createtime,"%Y-%m-%d") as createtime,info,author,clicks')->limit(6)->select();
            $this->assign('articleList4',$articleList4);
        }

        //热门阅读
        $hotArticleList=db('article')->order('clicks desc,sort desc,id desc')->field('id,title,thumb,createtime')->limit(4)->select();
        if(!empty($hotArticleList)){
            foreach ($hotArticleList as &$hot_v){
                $hot_v['articleCommentCount']=db('article_comment')->where('article_id='.$hot_v['id'])->count();
            }
            unset($v);
        }

        //最新评论
        $newArticleCommentList=db('article_comment')->alias('a')->join('user b','a.user_id = b.id')->order('a.id desc')->field('a.id,a.content,a.user_id,a.createtime,b.nickname,b.avatar,a.article_id')->select();
        if(!empty($newArticleCommentList)){
            foreach($newArticleCommentList as &$v){
                $v['timeDiffText']=format_date($v['createtime']);
                $v['createTimeText']=date('m-d',$v['createtime']);
            }
            unset($v);
        }

        $this->assign('pageInfo',$pageInfo);
        $this->assign('bannerList',$bannerList);
        $this->assign('businessCooperationList',$businessCooperationList);
        $this->assign('advertisingCooperationList',$advertisingCooperationList);
        $this->assign('categoryList1',$categoryList1);
        $this->assign('categoryList2',$categoryList2);
        $this->assign('categoryList3',$categoryList3);
        $this->assign('categoryList4',$categoryList4);
        $this->assign('hotArticleList',$hotArticleList);
        $this->assign('newArticleCommentList',$newArticleCommentList);
        return $this->view->fetch();
    }

}
